package edu.nepu.flink.api.checkpoint;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

import java.time.Duration;

/**
 * @Date 2024/3/3 16:37
 * @Created by chenshuaijun
 */
public class CheckpointConfDemo {

    public static void main(String[] args) throws Exception {

        Configuration configuration = new Configuration();

        // 启用最终检查点
        configuration.set(ExecutionCheckpointingOptions.ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH,true);

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(configuration);

        System.setProperty("HADOOP_USER_NAME","chenshuaijun");

        // 启用检查点: 指定Checkpoint的间隔时间（默认是500ms）,指定语义级别
        env.enableCheckpointing(2000, CheckpointingMode.EXACTLY_ONCE);
        CheckpointConfig checkpointConfig = env.getCheckpointConfig();
        // 设置Checkpoint的存储路径
        checkpointConfig.setCheckpointStorage("hdfs://hadoop102:8020/chk");
        // 设置Checkpoint的超时时间，默认是10min
        checkpointConfig.setCheckpointTimeout(10 * 60 * 1000);
        // 设置Checkpoint是否清理:一般设置为取消任务后仍保留到外部系统
        checkpointConfig.setExternalizedCheckpointCleanup(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);
        // 设置能够容忍的连续失败的次数，默认是0，我们一般设置的会大一点
        checkpointConfig.setTolerableCheckpointFailureNumber(10);
        // 设置最小间隔时间
        checkpointConfig.setMinPauseBetweenCheckpoints(1);
        // 设置当前最大Checkpoint的数量
        checkpointConfig.setMaxConcurrentCheckpoints(1);


        // 开启非对齐检查点
        /**
         * TODO 想要开启非对齐检查点，Checkpoint的语义级别必须是精确一次性，并且最大并发只能是1
         */
        checkpointConfig.enableUnalignedCheckpoints();
        // TODO 我们开启这个选项是有一个好处的，开始的时候会使用对其检查点，当对齐检查点的时间超时的话，会将其更改为非对齐检查点
        checkpointConfig.setAlignedCheckpointTimeout(Duration.ofSeconds(10));

        // 开启changelog
        env.enableChangelogStateBackend(true);

        env.socketTextStream("hadoop102",9999)
                .flatMap(new FlatMapFunction<String, Tuple2<String,Integer>>() {
                    @Override
                    public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
                        String[] split = value.split(" ");
                        for (String s : split) {
                            out.collect(Tuple2.of(s,1));
                        }
                    }
                })
                .keyBy(value -> value.f0)
                .sum(1)
                .print();
        env.execute();
    }
}
